package com.yitiao.multithread.case00;

/**
 * <h3>模拟cpu线程调度（切换）</h3>
 * <p>四个线程分别打印A、B、C、D，并不是按照启动的顺序依次执行</p>
 * <p>BBBBBBBBBBBBBBBBCCCCCCCCDDDDDDDDDDDDDDDDDDDDDAAAABB</p>
 */
public class CpuTaskDemo {
    public void printChar(String word){
        System.out.print(word);
    }

    public static void main(String[] args) {
        CpuTaskDemo c = new CpuTaskDemo();
        Thread taskA = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<100;i++){
                    c.printChar("A");
                }
            }
        });


        Thread taskB = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<100;i++){
                    c.printChar("B");
                }
            }
        });


        Thread taskC = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<100;i++){
                    c.printChar("C");
                }
            }
        });


        Thread taskD = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<100;i++){
                    c.printChar("D");
                }
            }
        });
        taskA.start();
        taskB.start();
        taskC.start();
        taskD.start();
    }
}
